package gait.cannon.dao;

import gait.cannon.bean.Article;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class ArticleDao implements IArticleDao {

	private JdbcTemplate jdbcTemplate;
	
	@Resource
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public void delete(Integer id) {
		String sql = "delete from article where id=?";
		Object[] args = new Object[] { id };
		jdbcTemplate.update(sql, args);
	}

	public void get(Integer id) {

	}

	public void insert(Article article) {
		String sql = "insert into article values (null, ?, ?)";
		Object[] args = new Object[] { article.getTitle(), article.getUrl() };
		jdbcTemplate.update(sql, args);
	}

	public List<Article> list() {
		String sql = "select * from article";
		return jdbcTemplate.query(sql, new RowMapper<Article>() {
			public Article mapRow(ResultSet rs, int arg) throws SQLException {
				Article article = new Article();
				article.setId(rs.getInt("id"));
				article.setTitle(rs.getString("title"));
				article.setUrl(rs.getString("url"));
				return article;
			}
		});
	}

	public void update(Article article) {

	}

}
